classdef UniformLinkRateGenerator < ClockHandler
    %UNIFORMLINKRATEGENERATOR Summary of this class goes here
    %   Detailed explanation goes here
    
    properties(Constant)
        %802.11g data rates
        link_rates_in_Mbps = [6, 9, 12, 18, 24, 36, 48, 54];
    end
    
    properties
        link_rate_indexes = [];
        last_rate_change_time_in_msec = 0;
        number_of_channels = 0;
    end
    
    methods
        function obj = UniformLinkRateGenerator(number_of_channels)
            if nargin>0
                if (number_of_channels~=0)
                    obj.number_of_channels = number_of_channels;
                    obj.link_rate_indexes = ones(number_of_channels);
                    obj.link_rate_indexes = obj.link_rate_indexes(1,:);
                end
                %randomise channel rates 
                for j=1:length(obj.link_rate_indexes)
                    % CR - use floor instead of round
					obj.link_rate_indexes(j) = round(rand*(length(UniformLinkRateGenerator.link_rates_in_Mbps)-1)) + 1;
                end
            end
            %register on clock tick
            ck = ClockSingleton.instance();
            ck.register_for_clock_ticks(obj);
        end
        
        function rate = get_link_rate_in_Kbps(obj, channel_index)
            assert(channel_index<=obj.number_of_channels);
            rate = (UniformLinkRateGenerator.link_rates_in_Mbps(obj.link_rate_indexes(channel_index)))*1024;
        end
        
        function clock_tick_handler(obj)
            ck = ClockSingleton.instance();
            curr_time_in_msec = ck.current_time_in_msec;
            constant_rate_period_in_msec = ParamsSingleton.instance().uniform_link_rate_generator_stability_period_in_msec;
            if (curr_time_in_msec - obj.last_rate_change_time_in_msec) > constant_rate_period_in_msec
                for j=1:length(obj.link_rate_indexes)
                    % CR - use floor instead of round
					obj.link_rate_indexes(j) = round(rand*(length(UniformLinkRateGenerator.link_rates_in_Mbps)-1)) + 1;
                end
                obj.last_rate_change_time_in_msec = curr_time_in_msec;
            end
        end
    end
    
    methods(Static)
        function static_clock_tick_handler(obj)
            obj.clock_tick_handler();
        end
    end
    
end

